<?php

// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      

//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/07/13
//      |_|                                         | TpMeCMS

declare(strict_types=1);

namespace ImiApp\ApiServer\Api\Controller\run;

use Imi\Db\Db;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use ImiApp\ApiServer\Api\Controller\user\RunBean;
// use Phpben\Imi\Validate\Annotation\Validate;
use Phpben\Imi\Auth\Annotation\Auth;


/**
 * 统计
 * @Auth(name="token",nologin={"no"})
 * @Controller("/run.test/")
 */
class Test extends RunBean
{
    protected $noNeedLogin = ['no'];
    protected $noNeedRight = ['*'];



    /**
     * 运营中心测试字符
     *
     * @Action
     * @return void
     */
    public function testZiFu()
    {
        list($符号, $数字) =  $this->获取符号('<100%');


        return $this->success('', $符号);
    }


    /**
     * 运营中心测试统计
     *
     * @Action
     * 
     *
     * @param string $date 日期
     * @param integer $run_id 运营中心 
     * @return void
     */
    public function count($date = null, $run_id = 1)
    {
        $run_id = 1;
        if (!$date)
            $date = date('Y-m-d');
        $data['上月累计激励金'] = $this->上月累计激励金($run_id, $date);
        $data['后台设置的激励金发放比例'] = $this->后台设置的激励金发放比例($run_id, $date);
        $data['当前激励金发放比例'] = $this->当前激励金发放比例($run_id, $date);

        $data['待分配激励金'] = $this->待分配激励金($run_id, $date);
        $data['本月累计激励金'] = $this->本月累计激励金($run_id, $date);

        $data['激励金结余'] = $this->激励金结余($run_id, $date);
        $data['上月消费总额'] = $this->上月消费总额($run_id, $date);
        $data['本月累计激励金'] = $this->本月累计激励金($run_id, $date);

        $data['本月消费总额'] = $this->本月消费总额($run_id, $date);
        $data['消费占比'] = $this->消费占比($run_id, $date);

        $data['消费增长率'] = $this->消费增长率($run_id, $date);


        $data['获取消费发放比例'] = $this->获取消费发放比例($run_id, $date); //_N
        $data['当前收益_消费'] = $this->当前收益_消费($run_id, $date);

        $data['上月营业总额'] = $this->上月营业总额($run_id, $date);
        $data['本月营业总额'] = $this->本月营业总额($run_id, $date);

        $data['营业占比'] = $this->营业占比($run_id, $date);
        $data['营业增长率'] = $this->营业增长率($run_id, $date);
        $data['获取营业发放比例'] = $this->获取营业发放比例($run_id, $date); //_T

        $data['当前收益_营业'] = $this->当前收益_营业($run_id, $date);












        return $this->success('', $data);
    }




    /**
     * 测试更新运营中心统计
     *
     * @Action
     * 
     *
     * @param string $date 日期
     * @return void
     */
    public function update_all_run($date = null)
    {
        $list = Db::name('run')->select()->getArray();
   
        foreach ($list as $key => $val) {
            // var_dump($val['id']);
            $data = $this->update_bill($date,$val['id'],true); 
        }

        var_dump($list);
       
        return $this->success($date ,$list);

    }


    /**
     * 运营中心测试统计
     *
     * @Action
     * 
     *
     * @param string $date 日期
     * @param integer $run_id 运营中心 
     * @return void
     */
    public function update_bill($date = null, $run_id = 1,$self=false)
    {
        $run_id = 1;
        if (!$date)
            $date = date('Y-m-d');

        $data['run_id'] = $run_id;
        $time = strtotime($date)+1;
        $data['year'] = date('Y',$time);
        $data['m'] = date('n',$time);
        $data['day'] = date('d',$time);
        $data['date'] = date('Y-m-d',$time);
        $res = Db::name('run_excite_money_bill')->whereEx($data)->find();

        $data['time']  = $time;
        $com_data = $data;
        $data['updatetime'] = time();
       



        $data['last_month_total_excite_money'] = $this->上月累计激励金($run_id, $date);
        $data['admin_set_excite_money_send_ratio'] = $this->后台设置的激励金发放比例($run_id, $date);
        $data['now_excite_money_send_ratio'] = $this->当前激励金发放比例($run_id, $date);

        $data['unallocate_excite_money'] = $this->待分配激励金($run_id, $date);
        $data['month_total_total_excite_money'] = $this->本月累计激励金($run_id, $date);

        $data['surplus_excite_money'] = $this->激励金结余($run_id, $date);
        $data['last_month_total_consume'] = $this->上月消费总额($run_id, $date);
 

        $data['month_total_consume'] = $this->本月消费总额($run_id, $date);
        $data['consume_ratio'] = $this->消费占比($run_id, $date);

        $data['consume_inc_ratio'] = $this->消费增长率($run_id, $date);


        $data['consume_send_ratio'] = $this->获取消费发放比例($run_id, $date); //_N
        $data['now_income_consume'] = $this->当前收益_消费($run_id, $date);

        $data['last_month_total_trade'] = $this->上月营业总额($run_id, $date);
        $data['month_total_trade'] = $this->本月营业总额($run_id, $date);

        $data['trade_ratio'] = $this->营业占比($run_id, $date);
        $data['trade_inc_ratio'] = $this->营业增长率($run_id, $date);
        $data['trade_send_ratio'] = $this->获取营业发放比例($run_id, $date); //_T

        $data['now_income_trade'] = $this->当前收益_营业($run_id, $date);
        if($res){
            Db::name('run_excite_money_bill')->where('id','=',$res['id'])->update($data);
            $msg = "更新成功";
        }else{
            Db::name('run_excite_money_bill')->insert($data);
            $msg = "加入成功";
        }

        $sys_user_list = $this->获取系统分配人员列表($run_id);
        foreach ($sys_user_list as $key => &$val) {
            $val['money'] = $this->运营中心获取用户分配当前收益($run_id, $date, $val['ratio']);

            
            $where_com = $com_data;
            $where_com['user_id'] = $val['user_id'];
            $where_com['status'] = 'sys';//状态:sys=系统添加,run=运营中心添加
            $where_com['bonus_ratio'] = $val['ratio'];
            $where_com['bonus'] = $val['money'];
            var_dump($where_com);
            
            $res = Db::name('run_excite_money_bill_user')->whereEx($com_data)->find();
            if($res){
                $where_com['updatetime'] = time();
                
                Db::name('run_excite_money_bill_user')->where('id','=',$res['id'])->update($where_com);
            }else{
                $where_com['time'] = $time;
                $where_com['updatetime'] = time();
                Db::name('run_excite_money_bill_user')->insert($where_com);
            }
        }

        $user_list = $this->获取分配人员和比例列表($run_id);
        foreach ($user_list as $key => &$val) {
            $val['money'] = $this->运营中心获取用户分配当前收益($run_id, $date, $val['ratio']);


            $where_com = $com_data;
            $where_com['user_id'] = $val['user_id'];
            $where_com['status'] = 'run';//状态:sys=系统添加,run=运营中心添加
            $where_com['bonus_ratio'] = $val['ratio'];
            $where_com['bonus'] = $val['money'];
            var_dump($where_com);
            
            $res = Db::name('run_excite_money_bill_user')->whereEx($com_data)->find();
            if($res){
                $where_com['updatetime'] = time();
                
                Db::name('run_excite_money_bill_user')->where('id','=',$res['id'])->update($where_com);
            }else{
                $where_com['time'] = $time;
                $where_com['updatetime'] = time();
                Db::name('run_excite_money_bill_user')->insert($where_com);
            }
        }


        
        // var_dump($data);
        if(!$self)
            return $this->success($msg, ['data'=>$data,'sys_user_list'=>$sys_user_list,'user_list'=>$user_list]);

        return $data;
    }
}
